package com.concurrence.interrupt;

/**
 * InterruptNotifyThread..   捕获到中断异常
 *
 * @author Lizhong
 * @date 2019/4/3
 */
public class InterruptNotifyThread {
    static InterruptNotifyThread interruptNotifyThread = new InterruptNotifyThread();

    public static void main(String[] args) throws InterruptedException {
        Thread go = new Thread(() -> {
            synchronized (interruptNotifyThread) {
                System.out.println("go");

                try {
                    interruptNotifyThread.wait();
                } catch (InterruptedException e) {
                    System.out.println("中断异常");
                }
            }
        });
        Thread go1 = new Thread(() -> {
            synchronized (interruptNotifyThread) {
                System.out.println("go");

                try {
                   for(int i=0;;i++){
                       int j=i*i/3+i;
                       // 中断只是一个标志位，并不会爆出异常
                       System.out.println(Thread.currentThread().isInterrupted());
                   }
                } catch (Exception e) {
                    System.out.println("中断异常");
                }
            }
        });
        go.start();
        go1.start();
        // 等待2m 是否打印over
        go.interrupt();
        go1.interrupt();
    }
}
